package com.jdls.tanhua.dubbo.impl;

import cn.hutool.core.collection.CollUtil;
import com.jdls.tanhua.dubbo.RecommendUserApi;
import com.jdls.tanhua.mongo.RecommendUser;
import com.jdls.tanhua.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/10/26
 */
@DubboService
public class RecommendUserImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public RecommendUser find(Long userId) {
        Criteria criteria = Criteria.where("toUserId").is(userId);
        Query query = Query.query(criteria);
        query.with(Sort.by(Sort.Order.desc("score"))).limit(1);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
//        RecommendUser recommendUser = (RecommendUser) recommendUsers

        System.out.println(recommendUsers.get(0));
        return recommendUsers.get(0);
    }

    @Override
    public List<RecommendUser> findAll(Long userId) {
        Criteria criteria = Criteria.where("toUserId").is(userId);
        Query query = Query.query(criteria);
        query.with(Sort.by(Sort.Order.desc("score")));
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        return recommendUsers;
    }

    @Override
    public List<RecommendUser> findAll(Long userId, List<Long> ids) {
        Query query = Query.query(Criteria.where("toUserId").is(userId).and("userId").in(ids));
        return null;
    }


    @Override
    public RecommendUser findOne(Long userId, Long toUserId) {

        Criteria criteria = Criteria.where("toUserId").is(toUserId).and("userId").is(userId);
        Query query = Query.query(criteria);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    @Override
    public List<RecommendUser> findLike(Long userId,int counts) {
        //1、查询喜欢不喜欢的用户ID
        List<UserLike> likeList = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> likeUserIdS = CollUtil.getFieldValues(likeList, "likeUserId", Long.class);
        //2、构造查询推荐用户的条件
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIdS);
        //3、使用统计函数，随机获取推荐的用户列表
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),//指定查询条件
                Aggregation.sample(counts)
        );
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
        //4、构造返回
        return results.getMappedResults();
    }
}
